import { map } from 'lodash';
import dayjs from 'dayjs';
import { getNextDayWeatherWithHours, getWeatherWithDays } from '@/api';
import { enWeek } from '@/utils/utils';

export default async function useWeather(city: string): Promise<any> {
  // 获取未来24小时以及当天和未来一星期的天气情况
  const [res, res2] = await Promise.all([getNextDayWeatherWithHours(city), getWeatherWithDays(city)]);
  return {
    city,
    current: {
      currentTemperature: res.results[0].hourly[0].temperature,
      maxTemperature: res2.results[0].daily[0].high,
      minTemperature: res2.results[0].daily[0].low,
      weather: res2.results[0].daily[0].text_day
    },
    hourArr: map(res.results[0].hourly, (item) => {
      return {
        time: dayjs().hour() === dayjs(item.time).hour() ? '现在' : dayjs(item.time).hour() + '时',
        weather: item.text,
        temperature: item.temperature + '℃'
      };
    }),
    weekArr: map(res2.results[0].daily, (item) => {
      return {
        time: dayjs().format('YYYY-MM-DD') === item.date ? '今天' : enWeek(dayjs(item.date).day()),
        weather: item.text_day,
        minTemperature: item.low + '℃',
        maxTemperature: item.high + '℃'
      };
    })
  };
}
